package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.RandomCharData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.GridAccount;
import com.ruoyi.system.domain.GridLogs;
import com.ruoyi.system.domain.GridPolicy;
import com.ruoyi.system.service.IGridAccountService;
import com.ruoyi.system.service.IGridLogsService;
import com.ruoyi.system.service.IGridPolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask {
    @Autowired
    private IGridAccountService gridAccountService;

    @Autowired
    private IGridPolicyService gridPolicyService;

    @Autowired
    private IGridLogsService gridLogsService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        System.out.println("执行无参方法");
        GridAccount gridAccount = new GridAccount();
        List<GridAccount> list = gridAccountService.selectGridAccountList(gridAccount);
        for (int i = 0; i < list.size(); i++) {
            GridAccount item = list.get(i);
            System.out.println("-------------------------" + i);
            System.out.println(item.getHostIp());
            System.out.println(item.getUserName());
            System.out.println(item.getUserPass());


            GridPolicy policy = gridPolicyService.selectGridPolicyByPolicyId(20);
            if (policy.getExeMethon().equals("2")) {//自动
                int minlen = Integer.parseInt(policy.getPassMin());
                int rule = Integer.parseInt(policy.getPassRule());
                GridLogs logs = new GridLogs();
                logs.setHostIp(item.getHostIp());
                logs.setHostName(item.getHostName());
                logs.setOldName(item.getUserName());
                logs.setOldPass(item.getUserPass());
                logs.setName(item.getUserName());
                String pass = "";
                if (rule == 1) {
                    pass = item.getNewUserPass();
                }else if (rule == 2) {
                    pass = RandomCharData.createRandomNumPass(minlen);
                } else if (rule == 3) {
                    pass = RandomCharData.createRandomCharPass(minlen);
                } else if (rule == 4) {
                    pass = RandomCharData.createRandomCharSymbolPass(minlen);
                }
                logs.setPass(pass);
                logs.setStatus("1");
                logs.setCreateBy("system");
                gridLogsService.insertGridLogs(logs);
                //是否超级权限改密
            }
        }
    }
}
